package beans;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class DAOCLass implements Serializable {

	private static final long serialVersionUID = 6171158984847946462L;

	public ArrayList<String> getData(String searchTerm, String searchOption){

		ArrayList<String> descList = new ArrayList<>();
		if(searchOption.equals("0"))
		{
			try
			{
				Class.forName("com.mysql.jdbc.Driver");
				Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/totalitea", "root", "root");
				Statement stat= con.createStatement();
				ResultSet res= stat.executeQuery("SELECT * FROM product WHERE description LIKE \"%"+ searchTerm +"%\"");



				while(res.next())
				{
					descList.add(res.getString("description")+"/"+res.getString("id"));
				}
				res.close();
				stat.close();
				con.close();
			}
			catch(SQLException exception)
			{
				System.out.println(exception.getMessage());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return descList;
		}
		else
		{
			try
			{
				Class.forName("com.mysql.jdbc.Driver");
				Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/totalitea", "root", "root");
				Statement stat= con.createStatement();
				ResultSet res= stat.executeQuery("SELECT * FROM product WHERE category LIKE \"%"+ searchTerm +"%\"");



				while(res.next())
				{
					descList.add(res.getString("description")+"/"+res.getString("id"));
				}
				res.close();
				stat.close();
				con.close();
			}
			catch(SQLException exception)
			{
				System.out.println(exception.getMessage());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return descList;

		}


	}
}
